package concurrent.chapter17;

import java.util.Random;

//搬运
public class TransportThread extends Thread{
    private final Channel channel;
    //定义为static，TransportThread会有一个以上，但Random始终只有一个，因为Random也是线程安全的
    private static final Random random=new Random(System.currentTimeMillis());

    public TransportThread(String name,Channel channel) {
        super(name);
        this.channel = channel;
    }

    @Override
    public void run() {
        for (int i = 0; true ; i++) {
            Request request=new Request(getName(),i);
            this.channel.put(request);
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
